package cn.danger.com.fragment.user;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.AppCompatButton;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import cn.danger.com.activity.CommonActivity;
import cn.danger.com.activity.R;
import cn.danger.com.fragment.BackHandledFragment;
import cn.danger.com.presenter.LoginPresenter;
import cn.danger.com.presenter.LoginPresenterImpl;
import cn.danger.com.util.Constants;
import cn.danger.com.view.CommonView;
import cn.danger.com.widget.ClearEditText;

/**
 * Created by 俊 on 2016/12/26.
 */
public class LoginFragment extends BackHandledFragment implements View.OnClickListener, CommonView {

    private boolean hadIntercept;
    private View view;
    private CommonActivity activity;
    private Toolbar toolbar;
    private ClearEditText phone_editText;
    private ClearEditText password_editText;
    private AppCompatButton login_button;
    private AppCompatButton forget_password_button;
    private AppCompatButton to_register_button;
    private ProgressDialog dialog;
    private LoginPresenter presenter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.fragment_login, container, false);

        bindView();
        bindEvent();

        return view;
    }

    @Override
    public void bindView() {
        activity = (CommonActivity) getActivity();
        toolbar = (Toolbar) view.findViewById(R.id.toolbar);
        phone_editText = (ClearEditText) view.findViewById(R.id.phone_editText);
        password_editText = (ClearEditText) view.findViewById(R.id.password_editText);
        login_button = (AppCompatButton) view.findViewById(R.id.login_button);
        forget_password_button = (AppCompatButton) view.findViewById(R.id.forget_password_button);
        to_register_button = (AppCompatButton) view.findViewById(R.id.to_register_button);

        dialog = new ProgressDialog(activity, ProgressDialog.THEME_HOLO_LIGHT);
        dialog.setMessage("正在执行...");
        dialog.setCanceledOnTouchOutside(false);

        presenter = new LoginPresenterImpl(activity, this);
    }

    @Override
    public void bindEvent() {
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                activity.finish();
            }
        });
        login_button.setOnClickListener(this);
        forget_password_button.setOnClickListener(this);
        to_register_button.setOnClickListener(this);
    }

    @Override
    public void showDialog() {
        dialog.show();
    }

    @Override
    public void hideDialog() {
        dialog.dismiss();
    }

    @Override
    public void showMessage(String message) {
        Toast.makeText(activity, message, Toast.LENGTH_SHORT).show();
        if (message.equals(Constants.SUCCESS_TO_LOGIN)) {
            Intent intent = new Intent("update_user");
            intent.putExtra("update", "all");
            activity.sendBroadcast(intent);
            activity.finish();
        } else if (message.equals(Constants.SUCCESS_TO_GET_USERDATA_FROM_SERVER)) {
            Intent intent = new Intent("update_ui");
            activity.sendBroadcast(intent);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.login_button:
                presenter.login(phone_editText.getText().toString(), password_editText.getText().toString());
                break;
            case R.id.forget_password_button:
                getFragmentManager().beginTransaction().addToBackStack(null).replace(R.id.container, new ForgetPasswordFragment()).commit();
                break;
            case R.id.to_register_button:
                getFragmentManager().beginTransaction().addToBackStack(null).replace(R.id.container, new RegisterFragment()).commit();
                break;
            default:
                break;
        }
    }

    @Override
    public boolean onBackPressed() {
        if (hadIntercept) {
            return false;
        } else {
            hadIntercept = true;
            return true;
        }
    }
}
